
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:yuan_hang_app/base/base_state.dart';
import 'package:yuan_hang_app/base/full_base_page.dart';
import 'package:yuan_hang_app/res/w_colors.dart';
import 'package:yuan_hang_app/res/w_dimens.dart';
import 'package:yuan_hang_app/utils/size_utils.dart';
import 'package:yuan_hang_app/widget/w_text.dart';

class WebViewPage extends StatefulWidget {
  String url;
  String title;
  WebViewPage( {super.key,required this.url,required this.title});

  @override
  ConsultationPageState createState() => ConsultationPageState();
}

class ConsultationPageState extends BaseState<WebViewPage> {
  bool isLoading = true;
  int progress = 0;
  late final WebViewController _controller;
  @override
  FullBasePage buildState(BuildContext context) {
    return FullBasePage(
        title: widget.title,
        backgroundColor: Colors.white,
        child: progress < 100
            ? Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              const CircularProgressIndicator(
                color: WColors.primaryColor,
              ),
              SizedBox(
                height: 10.px,
              ),
              WText(
                "加载中...",
                style: TextStyle(
                    color: WColors.mainTextColor,
                    fontSize: WDimens.tsLarge),
              )
            ],
          ),
        )
            : WebViewWidget(
          controller: _controller,
        ));
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    final WebViewController controller =
    WebViewController.fromPlatformCreationParams(
        const PlatformWebViewControllerCreationParams());
    controller
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(const Color(0x00000000))
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int p) {
            progress = p;
            setState(() {});
            print('-----------------(progress : $progress%)');
          },
          onPageStarted: (String url) {
            print('------------------Page started loading: $url');
          },
          onPageFinished: (String url) {
            print('------------------Page finished loading: $url');
          },
        ),
      );
    _controller = controller;
    _controller.loadRequest(Uri.parse(widget.url));
    // _controller.loadRequest(Uri.parse(
    //     "http://59.48.146.131:29180/document/view?viewToken=e54bf102-e2a5-4a12-90c6-3c5418f1969d"));

  }
}
